package com.uphone.kingmall.fragment.my;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.activity.personal.LiuYanReplyActivity;
import com.uphone.kingmall.base.BaseFragment;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : 王雨
 *     e-mail : sunzengyan@outlook.com
 *     time   : 2018/07/31
 *     desc   :留言
 *     version: 1.0
 * </pre>
 */
public class LiuYanFragment extends BaseFragment {

    String mTitle;
    @BindView(R.id.et_shop_name)
    EditText etShopName;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.btn_commit)
    Button btnCommit;
    @BindView(R.id.tv_reply)
    TextView tvReply;
    private int type = 0;

    public static LiuYanFragment getInstance(String title, int type) {
        LiuYanFragment rf = new LiuYanFragment();
        rf.mTitle = title;
        rf.type = type;
        return rf;
    }

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_fragment_liuyan;
    }

    @Override
    public void initData() {
        if (TextUtils.equals(mTitle, "留言")) {
            etShopName.setVisibility(View.GONE);
        }
        tvReply.setText(mTitle+"记录");
    }

    @Override
    public void initView(View view) {

    }

    @OnClick({R.id.tv_reply, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_reply:
                CommonUtil.startIntentType(getActivity(), LiuYanReplyActivity.class, type);

                break;
            case R.id.btn_commit:
                HttpParams params = new HttpParams();
                if (mTitle.equals("留言")) {
                    //留言
                    if (CommonUtil.checkViewEmpty(new String[]{"请输入您的意见"}, etContent)) {
                        return;
                    }
                    KeyboardUtils.hideSoftInput(getActivity());
                    params.put("content", etContent.getText().toString().trim() + "");
                    OkGoUtils.progressRequest(MyUrl.subFeed, getActivity(), params, new onNormalRequestListener() {
                        @Override
                        public void onSuccess(String response, int code) {
                            if (code == 0) {
                                ToastUtils.showShortToast(getActivity(), "留言成功!");
                                etContent.setText("");
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        CommonUtil.startIntentType(getActivity(), LiuYanReplyActivity.class, type);
                                    }
                                }, 500);
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {

                        }
                    });
                } else {
                    // 投诉
                    if (CommonUtil.checkViewEmpty(new String[]{"请输入店铺名称", "请输入您的意见"}, etShopName, etContent)) {
                        return;
                    }
                    params.put("shopName", etShopName.getText().toString().trim() + "");
                    params.put("repReason", etContent.getText().toString().trim() + "");
                    OkGoUtils.progressRequest(MyUrl.reportShopUser, getActivity(), params, new onNormalRequestListener() {
                        @Override
                        public void onSuccess(String response, int code) {
                            if (code == 0) {
                                ToastUtils.showShortToast(getActivity(), "投诉成功!");
                                etContent.setText("");
                                etShopName.setText("");
                                new Handler().postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        CommonUtil.startIntentType(getActivity(), LiuYanReplyActivity.class, type);
                                    }
                                }, 500);
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {

                        }
                    });
                }
                break;
        }
    }
}
